<script setup>
  import { ref, computed } from 'vue'
  import { onLoad } from '@dcloudio/uni-app'
  import { delayApi } from '@/apis/task.js'

  // 1. 接受数据
  const id = ref('')
  const planDepartureTime = ref('') // 计划提货时间(yyyy-MM-dd HH:mm:ss)
  const startTime = ref('') // 计划提货时间(HH:mm:ss)
  onLoad((params) => {
    id.value = params.id
    planDepartureTime.value = params.planDepartureTime
    const [, time] = params.planDepartureTime.split(' ')
    startTime.value = time
  })

  // 2. 选择延迟时间
  const delayTime = ref('')
  const onChangeTime = (e) => {
    const [date] = planDepartureTime.value.split(' ')
    delayTime.value = `${date} ${e.detail.value}`
  }
  const delayTimeValid = computed(() => {
    // 0 < 延迟的时间 - 原定时间 <= 2(2  * 60 * 60 * 1000)
    const start = new Date(planDepartureTime.value)
    const end = new Date(delayTime.value)
    return end - start > 0 && end - start <= 2 * 60 * 60 * 1000
  })

  // 3. 延迟原因
  const delayReason = ref('')
  const wordCount = computed(() => delayReason.value.length)
  const delayResonValid = computed(() => {
    return wordCount.value > 0 && wordCount.value <= 50
  })

  // 4. 控制按钮的可用状态
  // 返回false按钮可用, 返回true按钮不可用
  const submitDisabled = computed(() => {
    return !delayTimeValid.value || !delayResonValid.value
  })

  // 5. 调用延迟提货接口
  const onSubmit = async () => {
    const formData = {
      id: id.value,
      delayTime: delayTime.value,
      delayReason: delayReason.value,
    }
    const { code } = await delayApi(formData)
    if (code !== 200) return uni.utils.toast('延迟提货失败~')
    uni.reLaunch({
      url: '/pages/task/index',
    })
  }
</script>

<template>
  <view class="page-container">
    <uni-list :border="false">
      <uni-list-item
        title="原定时间 "
        showArrow
        :rightText="planDepartureTime"
      />
      <uni-list-item title="延迟时间" showArrow>
        <template v-slot:footer>
          <picker
            :start="startTime"
            class="time-picker"
            mode="time"
            @change="onChangeTime"
          >
            <text v-if="delayTime" :class="{ error: !delayTimeValid }"
              >{{ delayTime }}:00</text
            >
            <text v-else>不可超过2个小时</text>
          </picker>
        </template>
      </uni-list-item>
      <uni-list-item direction="column">
        <template v-slot:body>
          <view class="textarea-wrapper">
            <textarea
              v-model="delayReason"
              class="textarea"
              placeholder-style="color: #818181"
              placeholder="请输入延迟提货原因"
            ></textarea>
            <text class="words-count" :class="{ error: wordCount > 50 }"
              >{{ wordCount }}/50</text
            >
          </view>
        </template>
      </uni-list-item>
      <uni-list-item :border="false">
        <template v-slot:body>
          <button :disabled="submitDisabled" class="button" @click="onSubmit">
            提交
          </button>
        </template>
      </uni-list-item>
    </uni-list>
  </view>
</template>

<style lang="scss" scoped>
  @import './index.scss';
</style>
